﻿Public Class Administrativo
    Dim arr As Hashtable
    Private contador As Integer
    Dim cv As Integer

    'Generacion de numero aleatorio
    Private Function Aleatorio(ByVal Minimo As Long, ByVal Maximo As Long) As Long
        Randomize()
        Aleatorio = CLng((Minimo - Maximo) * Rnd() + Maximo)
        Return Aleatorio
    End Function

    'Buscar
    Private Sub Buscar(ByVal cadena As String, ByVal flag As Integer, ByVal filtro As String)
        Dim cont As Integer = 0
        ListBox1.Items.Clear()
        For Each item As Object In arr.Values
            If flag = 0 Then
                If (cadena <> "") Then
                    If filtro <> "" Then
                        If (CType(item, Curso).p_nombre).StartsWith(cadena) And (CType(item, Curso).p_tipo).StartsWith(filtro) Then
                            ListBox1.Items.Add(CType(item, Curso).p_nombre)
                            RichTextBox2.Text = ""
                            cont = cont + 1
                        End If
                    Else
                        If (CType(item, Curso).p_nombre).StartsWith(cadena) Then
                            ListBox1.Items.Add(CType(item, Curso).p_nombre)
                            RichTextBox2.Text = ""
                            cont = cont + 1
                        End If
                    End If
                Else
                    If filtro <> "" Then
                        If (CType(item, Curso).p_tipo).StartsWith(filtro) Then
                            ListBox1.Items.Add(CType(item, Curso).p_nombre)
                            RichTextBox2.Text = ""
                            cont = cont + 1
                        End If
                    Else
                        ListBox1.Items.Add(CType(item, Curso).p_nombre)
                        RichTextBox2.Text = ""
                        cont = cont + 1
                    End If
                End If
            End If
        Next

        If cont = 0 Then
            ListBox1.Items.Clear()
            info.Text = "No se han encontrado coincidencias"
        Else
            info.Text = "Se encontraron " & cont & " coincidencias"
        End If
    End Sub

    'Load
    Private Sub Administrativo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ComboBox1.SelectedIndex = 0
        Label10.Text = Aleatorio(0, 1000000)
        arr = New Hashtable
        label2.Text = Form1.tb_usuario.Text
        info.Text = "Aplicación iniciada"
        barra.Visible = False
        Button2.Enabled = False
        Button3.Enabled = False
        If MsgBox("Desea añadir cursos de prueba", MsgBoxStyle.OkCancel, "Añadir Cursos") = MsgBoxResult.Ok Then
            Dim c As Curso = New Curso("Curso de Programacion en Moviles Android", 12333221, 1, "Curso", "cursos@urjc.es", "Mostoles", "13/03/2012", "www.urjc.cursos.es", "Cursos abierto")
            arr.Add(contador, c)
            contador = contador + 1
            c = New Curso("Escursion por la Pedriza", 23423421, 1, "Salida", "delegacionalumnos@urjc.es", "Mostoles", "13/03/2012", "www.urjc.cursos.es", "Cursos abierto")
            arr.Add(contador, c)
            contador = contador + 1
            c = New Curso("Seminario de Artes Graficas", 12312312, 1, "Seminario", "seminarios@urjc.es", "Funelabrada", "13/03/2012", "www.urjc.cursos.es", "")
            arr.Add(contador, c)
            contador = contador + 1
            c = New Curso("Curso de inicio a Photoshop", 65776867, 1, "Curso", "cursos@urjc.es", "Mostoles", "13/03/2012", "www.urjc.cursos.es", "Cursos abierto")
            arr.Add(contador, c)
            contador = contador + 1
            c = New Curso("Taller de desarrollo", 8906578, 1, "Taller", "cursos@urjc.es", "Mostoles", "13/03/2012", "www.urjc.cursos.es", "Cursos abierto")
            arr.Add(contador, c)
            contador = contador + 1
            c = New Curso("Concurso de tapas", 5685, 1, "Concurso", "delegacionalumnos@urjc.es", "Mostoles", "13/03/2012", "www.urjc.cursos.es", "Con premios")
            arr.Add(contador, c)
            contador = contador + 1

        End If

    End Sub

    'Boton Guardar
    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click, GuardarToolStripMenuItem.Click
        Dim c As Curso = New Curso(TextBox1.Text, Label10.Text, TextBox2.Text, ComboBox1.Text, TextBox4.Text, TextBox6.Text, DateTimePicker1.Text, TextBox5.Text, RichTextBox1.Text)
        arr.Add(contador, c)
        contador = contador + 1
        barra.Visible = False
        info.Text = "Curso Guardado"
        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox4.Text = ""
        TextBox5.Text = ""
        TextBox6.Text = ""
        RichTextBox1.Text = ""

    End Sub

    'Boton Buscar
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        barra.Visible = True
        barra.Value = 0
        info.Text = "Buscando..."
        Buscar(TextBox7.Text, 0, "")
        barra.Value = 100
    End Sub

    'Activar / Dasactivar guardar
    Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp, TextBox2.KeyUp, TextBox6.KeyUp, TextBox5.KeyUp, TextBox4.KeyUp, RichTextBox1.KeyUp
        If TextBox1.Text <> "" And TextBox2.Text <> "" And TextBox6.Text <> "" Then
            ToolStripButton1.Enabled = True
            GuardarToolStripMenuItem.Enabled = True
        Else
            ToolStripButton1.Enabled = False
            GuardarToolStripMenuItem.Enabled = False
        End If
    End Sub

    'Lista de busqueda
    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        For Each item As Object In arr.Values
            If (CType(item, Curso).p_nombre) = ListBox1.SelectedItem Then
                RichTextBox2.Text = "ID: " & CType(item, Curso).p_id & vbNewLine & _
                    "Nombre: " & CType(item, Curso).p_nombre & vbNewLine & _
                    "Numero de Creditos: " & CType(item, Curso).p_creditos & vbNewLine & _
                    "Tipo: " & CType(item, Curso).p_tipo & vbNewLine & _
                    "Lugar: " & CType(item, Curso).p_lugar & vbNewLine & _
                    "Fecha: " & CType(item, Curso).p_fecha & vbNewLine & _
                    "E-mail: " & CType(item, Curso).p_correo & vbNewLine & _
                    "Pagina Web: " & CType(item, Curso).p_pagina & vbNewLine & _
                    "Informacion Adicional: " & CType(item, Curso).p_info & vbNewLine
                cv = arr.Count
            End If
        Next
    End Sub

    'Eliminar Curso
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If MsgBox("Se va a porceder a eliminar el curso " & ListBox1.SelectedItem.ToString & ". No podrá deshacer los cambios", MsgBoxStyle.OkCancel, "Eliminacion de cliente") = MsgBoxResult.Ok Then
            arr.Remove(cv - 1)
            info.Text = "Curso " & ListBox1.SelectedItem.ToString & " eliminado"
            ListBox1.Items.Clear()
            RichTextBox2.Text = ""
            Buscar(TextBox7.Text, 0, "")
        Else
            info.Text = "Operacion cancelada"
        End If
    End Sub

    'Filtro
    Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
        If ComboBox2.SelectedIndex = 0 Then
            Buscar(TextBox7.Text, 0, "")
        Else
            Buscar(TextBox7.Text, 0, ComboBox2.Text)
        End If
    End Sub

    'RichTexbox 2 no escribir
    Private Sub RichTextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles RichTextBox2.KeyPress, ComboBox1.KeyPress, ComboBox2.KeyPress
        e.Handled = True
    End Sub




    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Modificar_Curso.ShowDialog()
    End Sub
    
    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
        If (Not IsNumeric(TextBox2.Text)) Then
            ErrorProvider1.SetError(TextBox2, "Este campo debe ser un número")
        Else
            ErrorProvider1.Clear()

        End If

    End Sub

    Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged
        If DateTimePicker1.Value < Now Then
            ErrorProvider1.SetError(DateTimePicker1, "No puede seleccionar una fecha anterior a la actual")
        End If
    End Sub

End Class